package com.timesheet.gui.efforttracker.editingsupport;

import com.example.timesheet.uitl.TaskUtils;
import com.timesheet.server.common.models.view.EffortTrackerViewModel;
import com.timesheet.server.common.models.view.EffortTrackerViewModelContainer;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxViewerCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;

public class TaskNoEditingSupport extends EditingSupport {

	private TableViewer tableViewer = null;

	private ComboBoxViewerCellEditor comboBoxViewerCellEditor = null;

	private boolean isEditable = false;

	public TaskNoEditingSupport(TableViewer viewer) {
		super(viewer);
		this.tableViewer = viewer;
		this.comboBoxViewerCellEditor = new ComboBoxViewerCellEditor(tableViewer.getTable());
		//comboBoxViewerCellEditor.getViewer().getCombo().setText(TaskUtils.getTaskStatus()[0]);
		comboBoxViewerCellEditor.setLabelProvider(new LabelProvider());
		comboBoxViewerCellEditor.setContentProvider(new ArrayContentProvider());
		comboBoxViewerCellEditor.setInput(TaskUtils.getTaskStatus());

	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		return comboBoxViewerCellEditor;
	}

	@Override
	protected boolean canEdit(Object element) {
		return isEditable;
	}

	public void setEditable(boolean isEditable) {
		this.isEditable = isEditable;
		canEdit(null);
		tableViewer.refresh();
	}

	@Override
	protected Object getValue(Object element) {

		if (element instanceof EffortTrackerViewModelContainer) {
			EffortTrackerViewModelContainer container = (EffortTrackerViewModelContainer) element;
			EffortTrackerViewModel effortTrackerViewModel = container.getDto();
			return effortTrackerViewModel.getStatus();

		}
		return null;
	}

	@Override
	protected void setValue(Object element, Object value) {

		if (element instanceof EffortTrackerViewModel) {
			EffortTrackerViewModel effortTrackerViewModel = (EffortTrackerViewModel) element;
			String newValue = (String) value;
			String taskType = effortTrackerViewModel.getTaskType();
			if (taskType != null && !taskType.equals(newValue)) {
				effortTrackerViewModel.setStatus(newValue);
				tableViewer.update(element, null);
			}

		}
	}

}
